//
// Created by afly on 20-12-28.
// Mail: a_fly0505@163.com
// QQ: 785230837 WX: afly003
// Company: juefx
//

#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__

#include <mutex>
#include <list>
#include <queue>
#include <vector>
#include <memory>
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <future>
#include <functional>

#include "utils/noncopyable.h"

namespace qsm {

    using ProcFuncNode_t = std::function<int(pthread_t)> ;

    class ThreadPool : public noncopyable {
    public:
        ThreadPool(const char *name);

        virtual ~ThreadPool();

        int Start(int threadNum);

        bool IsStart() const;

        int GetThreadNum() const;

        int GetTaskNums();

        void SetMaxTaskNums(int maxTaskNum);

        int Pause();

        int Continue();

        int Stop();

        int Commit(ProcFuncNode_t tasknode);

    private:
        void task_func();

    private:
        std::string m_ThreadPoolName;
        std::atomic_bool m_IsRun;
        std::atomic_bool m_IsPause;
        std::atomic_int_fast32_t m_FaileNum;
        std::atomic_int_fast32_t m_TotalNum;

        int m_ThreadNum = 0;
        int m_MaxTaskNum = 0;
        std::mutex m_QueueMutex;
        std::condition_variable m_Condition;
        std::vector<std::thread *> m_Workers;
        std::list<ProcFuncNode_t> m_Tasks;

        std::mutex m_PauseMutex;
        std::condition_variable m_PauseCondition;
    };
}
#endif //__THREAD_POOL_H__
